package com.techfengs.test01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test2 {
    public static void main (String[] args) throws ClassNotFoundException, SQLException {
        // 加载驱动
        Class.forName("org.mariadb.jdbc.Driver");
        // Class.forName("com.mysql.cj.jdbc.Driver");
        // 获取连接
        String url = "jdbc:mysql://192.168.3.99:3306/techfengs?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root";
        String password = "gg1111121";
        Connection conn = DriverManager.getConnection(url, username, password);
        // 创建会话
        Statement sta = conn.createStatement();
        // 发送SQL
        String sql = "select * from t_book";
        ResultSet rs = sta.executeQuery(sql);
        // 处理结果
        System.out.println("id\tname\t\t\tauthor\t\t\t\tprice");
        
        while ( rs.next() ) {
            System.out.print(rs.getInt("id") + "\t");
            System.out.print(rs.getString("name") + "\t");
            System.out.print(rs.getString("author") + "\t\t\t");
            System.out.println(rs.getDouble("price"));
        }
        // 关闭资源
        sta.close();
        conn.close();
    }
}
